package demo1;

import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public int score;

    public Student(String name, int age, int score) {
        this.name = name;//构造方法
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {//重写compareTo方法
        return this.age - o.age;//从小到大
        //return o.age - this.age;//从大到小
    }
}
/*class Student {
    public String name;
    public int age;
    public int score;

    public Student(String name, int age, int score) {
        this.name = name;//构造方法
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}*/
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {//实现compare方法
        return o1.age - o2.age;
    }
}
class ScoreComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.score - o2.score;//从小到大 升序
    }
}
class NameComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10,19);
        students[1] = new Student("abc",8,78);
        students[2] = new Student("lisi",13,57);
        //比较器
        AgeComparator ageComparator = new AgeComparator();

        ScoreComparator scoreComparator = new ScoreComparator();

        NameComparator nameComparator = new NameComparator();


        Arrays.sort(students);//排序
        System.out.println(Arrays.toString(students));
    }
    public static void main1(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10,19);
        students[1] = new Student("abc",8,78);
        students[2] = new Student("lisi",13,57);
        /*if (students[0].compareTo(students[1]) > 0) {
            System.out.println("students[0] > students[1]");
        }*/
        Arrays.sort(students);//排序
        System.out.println(Arrays.toString(students));
    }
}
